﻿// See https://aka.ms/new-console-template for more information

using GossipCore;

(string, Dictionary<string, string>) GetConfig()
{
    // 使用 ConfigLoader 加载配置
    var config = new ConfigLoader();
    var peersDict = config.GetDict("cluster.peers");

    Console.WriteLine("Loaded cluster.peers from config file:");
    foreach (var kv in peersDict)
    {
        Console.WriteLine($"{kv.Key}: {kv.Value}");
    }

    // 使用 ArgsHelper 解析启动参数
    var argsHelper = new ArgsHelper(args);
    var nodeId = argsHelper.GetArg("id");
    if (string.IsNullOrEmpty(nodeId))
    {
        Console.WriteLine("未指定节点id，例如 --id=A");
        Console.WriteLine("按任意键退出...");
        Console.ReadKey();
        System.Environment.Exit(1);
    }
    Console.WriteLine($"当前节点id: {nodeId}");
    return (nodeId, peersDict);
}

try
{
    var (nodeId,peersDict) = GetConfig();
    var cluster = new HazeCluster(nodeId, peersDict);
    cluster.Start();
    
}
catch (Exception ex)
{
    Console.WriteLine("程序发生异常：" + ex);
}
Console.WriteLine("按任意键退出...");
Console.ReadKey();
